﻿using MassTransit;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Tiens.NotificationHub.Constant;
using Tiens.NotificationHub.IntegrationEvents;

namespace Tiens.NotificationHub.Core.Consumers
{
    public class NotifyingUserConsumer : IConsumer<NotifyingUserEvent>
    {
        private readonly IHubContext<NotificationsHub> _hubContext;

        public NotifyingUserConsumer(IHubContext<NotificationsHub> _hubContext)
        {
            this._hubContext = _hubContext;
        }

        public async Task Consume(ConsumeContext<NotifyingUserEvent> context)
        {
            var groupName = context.Message.UserId.ToGroupName(context.Message.AppId);
            await _hubContext.Clients.Groups(groupName).SendAsync(nameof(NotifyingUserEvent), context.Message.Message);
        }
    }
}
